﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;

namespace NMVN.Common
{
    public class AutoCompleteComboBoxCell : DataGridViewTextBoxCell
    {
        private object displayValue = null;
        private object value1 = null;       

        public AutoCompleteComboBox EditingAutoCompleteComboBox
        {
            get
            {
                return this.DataGridView.EditingControl as AutoCompleteComboBox;
            }
        }

        public override Type EditType
        {
            get
            {
                return typeof(AutoCompleteComboBoxEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                return typeof(object);
            }
        }

        public object EditingControlFormattedValue
        {
            get
            {
                return this.Value1;
            }
            set
            {
                this.Value1 = value;
            }
        }        

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);        

            
            
            //Cast the EditingControl to a variable we can work with
            AutoCompleteComboBoxEditingControl cEditBox = DataGridView.EditingControl as AutoCompleteComboBoxEditingControl;
            if (cEditBox != null)
            {                
                if (this.OwningColumn != null && (AutoCompleteComboBoxColumn)base.OwningColumn != null)
                {                    
                    cEditBox.DataSource = ((AutoCompleteComboBoxColumn)base.OwningColumn).DataSource;                 
                    cEditBox.ValueMember = ((AutoCompleteComboBoxColumn)base.OwningColumn).ValueMember;
                    cEditBox.DisplayMember = ((AutoCompleteComboBoxColumn)base.OwningColumn).DisplayMember;                    
                }

                cEditBox.OwnerCell = this;     
            }
        }


        public object Value1
        {
            get
            {
                return value1;
            }
            set
            {
                value1 = value;
            }
        }


        public object DisplayValue
        {
            get
            {
                return displayValue;
            }
            set
            {
                displayValue = value;
            }
        }
 

        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
        }

        public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
        {
            return this.Value1;            
        }

        protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            if (formattedValue != null)
                this.DisplayValue = FindDisplayValue(((AutoCompleteComboBoxColumn)this.OwningColumn).DataSource, ((AutoCompleteComboBoxColumn)this.OwningColumn).ValueMember, ((AutoCompleteComboBoxColumn)this.OwningColumn).DisplayMember, formattedValue.ToString());            
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, this.DisplayValue, errorText, cellStyle, advancedBorderStyle, paintParts);            
        }

        private string FindDisplayValue(DataTable dataTable, string valueMember, string displayMember, string value)
        {
            string stringValue = value.Trim();

            if (!string.IsNullOrWhiteSpace(value) && stringValue != "System.Data.DataRowView")
            {
                DataRow[] dr = dataTable.Select(valueMember + " = " + value);                
                if (dr != null)
                {                                       
                    stringValue = dr[0][displayMember].ToString().Trim();                    
                }
            }

            return stringValue;
        }

    }
}
